import { Statistic } from 'antd';
import _ from 'lodash';
import React from 'react';

const NULL_TYPE = [null, undefined, 'null', '', 'null', 'undefined'];
export default function RenderValue(props) {
  let node = '-';
  const { text, valueType, precision } = props?.cellData;
  if (!NULL_TYPE.includes(text)) {
    // 默认值 不做任何处理
    if (!valueType || valueType === 'text') {
      node = text;
    }
    // 金额
    if (valueType === 'money') {
      const val = _.toNumber(text);
      if (!_.isNaN(val)) {
        node = <Statistic value={val.toFixed(precision)} />;
      }
    }
    // 百分比
    if (valueType === 'percent') {
      const val = _.toNumber(text);
      if (!_.isNaN(val)) {
        node = <Statistic value={val.toFixed(precision)} suffix="%" />;
      }
    }
    // 正负差异
    if (valueType === 'positiveAndNegativeDiff') {
      let val = _.toNumber(text);
      if (!_.isNaN(val)) {
        val = val.toFixed(precision);
        let prefix = '';
        let color = 'rgba(0, 0, 0, 0.65)';
        if (val > 0) {
          prefix = '+';
          color = 'rgba(255, 0, 25, 1)';
        }
        if (val < 0) {
          prefix = '-';
          color = 'rgba(67, 207, 124, 1)';
        }
        node = <Statistic value={Math.abs(val)} valueStyle={{ color }} prefix={prefix} />;
      }
    }
    // 比列差异
    if (valueType === 'percentDiff') {
      let val = _.toNumber(text);
      if (!_.isNaN(val)) {
        let prefix = '';
        let color = 'rgba(0, 0, 0, 0.65)';
        if (val > 0) {
          prefix = '+';
          color = 'rgba(255, 0, 25, 1)';
        }
        if (val < 0) {
          val *= -1;
          prefix = '-';
          color = 'rgba(67, 207, 124, 1)';
        }
        node = (
          <Statistic
            value={val.toFixed(precision)}
            valueStyle={{ color }}
            prefix={prefix}
            suffix="%"
          />
        );
      }
    }
  }
  return node;
}
